/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controlador;

/**
 *
 * @author felumpe
 */
import com.sun.media.jai.codec.FileSeekableStream;
import com.sun.media.jai.codec.TIFFDecodeParam;
import java.awt.Frame;
import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.awt.image.renderable.ParameterBlock;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import javax.media.jai.RenderedOp;
import javax.media.jai.widget.ScrollingImagePanel;
  
public class Tiffeador {  
  
public static void main(String[] args)  
{    
    File file = new File("C:/Users/felumpe/Downloads/GEOTIFF/lol.tif");  
    FileSeekableStream stream = null;  
    try  
    {  
    stream = new FileSeekableStream(file);  
    } catch (IOException e)  
    {  
    e.printStackTrace();  
    System.exit(0);  
    }  

    ParameterBlock params = new ParameterBlock();  
    params.add(stream);  

    TIFFDecodeParam decodeParam = new TIFFDecodeParam();  
    decodeParam.setDecodePaletteAsShorts(true);  


    RenderedOp image1 = JAI.create("tiff", params, null);  

    int dataType = image1.getSampleModel().getDataType();  


    RenderedOp image2 = null;  
    if (dataType == DataBuffer.TYPE_BYTE)  
    {  
    System.out.println("TIFF image is type byte.");  
    image2 = image1;  
    } else  
    {  
    System.out.println("TIFF image is type " + dataType +  
    ", and will not be displayed.");  
    System.exit(0);  
    }  

    int width = image2.getWidth();  
    int height = image2.getHeight();  

    ScrollingImagePanel panel = new ScrollingImagePanel(image2, width, height);  

    Frame window = new Frame("ImagemTIFF");  
    window.add(panel);  
    window.pack();  
    window.show();  
    }

    public static void Compositor(File R, File G, File B) throws IOException 
    {
        // abrir a imagem em renderedop, converter pra planar e depois bufferedimage
        FileSeekableStream stream = null;   
        try  
        {  
            stream = new FileSeekableStream(R);  
        } catch (IOException e) {System.exit(0);}  
        ParameterBlock params = new ParameterBlock();  
        params.add(stream);  
        TIFFDecodeParam decodeParam = new TIFFDecodeParam();  
        decodeParam.setDecodePaletteAsShorts(true);  
        RenderedOp image1 = JAI.create("tiff", params, null);
        PlanarImage image1aux = image1.createInstance();        
        BufferedImage Raux = new BufferedImage(image1aux.getWidth(), image1aux.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
        Raux.setData(image1aux.getData());
        image1.dispose();
        image1aux.dispose();
        System.gc();
        try  
        {  
            stream = new FileSeekableStream(G);  
        } catch (IOException e) {System.exit(0);}  
        params = new ParameterBlock();  
        params.add(stream);  
        decodeParam = new TIFFDecodeParam();  
        decodeParam.setDecodePaletteAsShorts(true);  
        RenderedOp image2 = JAI.create("tiff", params, null);
        PlanarImage image2aux = image1.createInstance();   
        BufferedImage Gaux = new BufferedImage(image2aux.getWidth(), image2aux.getHeight(), BufferedImage.TYPE_BYTE_GRAY);   
        Gaux.setData(image2aux.getData());        
        image2.dispose();
        image2aux.dispose();
        System.gc();
        try  
        {  
            stream = new FileSeekableStream(R);  
        } catch (IOException e) {System.exit(0);}  
        params = new ParameterBlock();  
        params.add(stream);
        decodeParam = new TIFFDecodeParam();  
        decodeParam.setDecodePaletteAsShorts(true);  
        RenderedOp image3 = JAI.create("tiff", params, null);
        PlanarImage image3aux = image1.createInstance();        
        BufferedImage Baux = new BufferedImage(image3aux.getWidth(), image3aux.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
        Baux.setData(image3aux.getData());
        image3.dispose();
        image3aux.dispose();
        System.gc();
        
        //faz a composição, através dos deslocamentos das faixas de bits 0-8 8-16 16-24 e atribui a uma nova ibagen    
        BufferedImage retorno = new BufferedImage(image1aux.getWidth(), image1aux.getHeight(), BufferedImage.TYPE_INT_RGB);
        for(int i =0; i<retorno.getWidth(); i++)
        {
            for(int j=0; j<retorno.getHeight(); j++)
            {
                retorno.setRGB(i, j, (Raux.getRGB(i, j) & 0xFF + Gaux.getRGB(i, j) & 0xFF00 + Baux.getRGB(i, j) & 0xFF0000));
            }
        }
        //ImageIO.write(retorno, "jpg", new File("lulz.jpg"));
    }
}  